<template>
  <div class="app-container">
    <el-tabs :active-name="activeClientType" v-model="activeClientType" @tab-click="handleClientType">
      <el-tab-pane :label="item.label+'('+item.value+')'" :name="item.name" v-for="item in clientTypeList"
                   :key="item.name"
      ></el-tab-pane>
    </el-tabs>

    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">

      <el-form-item label="姓名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电话" prop="userPhone">
        <el-input
          v-model="queryParams.userPhone"
          placeholder="请输入用户手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="录入时间" prop="createTime">
        <el-date-picker
          v-model=dateRange
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-tabs :active-name="activeCluePoolState" v-model="activeCluePoolState" @tab-click="handleActiveCluePoolState">
      <el-tab-pane :label="item.label+'('+item.value+')'" :name="item.name" v-for="item in statusList" :key="item.name"
      ></el-tab-pane>
    </el-tabs>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['enterprise:cluePool:add']"
        >录入客源
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>


    <el-table v-loading="loading" :data="cluePoolList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" width="55" align="center">
        <template slot-scope="scope">{{
            (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1
          }}
        </template>
      </el-table-column>

      <el-table-column label="姓名" align="center" prop="userName"/>
      <el-table-column label="电话" align="center" prop="userPhone"/>
      <el-table-column label="意向地址" align="center" prop="premisesAddress"/>
      <el-table-column label="需求来源" align="center" prop="cluePoolSource">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.clue_pool_source" :value="scope.row.cluePoolSource"/>
        </template>
      </el-table-column>
      <el-table-column label="紧急程度" align="center" prop="urgencyDegree">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.urgency_degree" :value="scope.row.cluePoolSource"/>
        </template>
      </el-table-column>
      <el-table-column label="需求信息" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.minimumPrice&&scope.row.topPrice"> {{
              scope.row.minimumPrice + '元~' + scope.row.topPrice + '元'
            }}</span>
          <span v-else>-</span>
          <dict-tag :options="dict.type.house_type" :value="scope.row.houseType"/>
        </template>
      </el-table-column>
      <el-table-column label="最新跟进时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span v-if="scope.row.updateTime">{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="录入人" align="center" prop="createBy"/>
      <el-table-column label="录入时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span v-if="scope.row.createTime">{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="cluePoolState">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.clue_pool_state" :value="scope.row.cluePoolState"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="mini"
            @click="handleDetail(scope.row)"
            v-hasPermi="['enterprise:cluePool:query']"
          >
            详情
          </el-button>
          <el-button
            type="text"
            size="mini"
            @click="handleEdit(scope.row)"
            v-hasPermi="['enterprise:cluePool:edit']"
          >
            {{ scope.row.cluePoolState === '1' ? '编辑' : '激活' }}
          </el-button>
          <el-button
            type="text"
            size="mini"
            @click="handleCloseCluePool(scope.row)"
            v-if="(scope.row.cluePoolState === '1'&&queryParams.roleType === '1')||(activeClientType === '2'&&scope.row.cluePoolState === '1')"
            v-hasPermi="['enterprise:cluePool:close']"
          >
            关闭
          </el-button>
          <el-button
            type="text"
            size="mini"
            @click="handleClaimCluePool(scope.row)"
            v-if="scope.row.cluePoolState === '1'&&queryParams.roleType === '2'&&activeClientType === '1'"
            v-hasPermi="['enterprise:cluePool:close']"
          >
            认领
          </el-button>
          <el-button
            type="text"
            size="mini"
            @click="handleTransferToPublic(scope.row)"
            v-if="activeClientType === '2'"
          >
            转为公盘
          </el-button>
        </template>
      </el-table-column>

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!--    &lt;!&ndash; 添加或修改线索池信息对话框 &ndash;&gt;-->
    <!--    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>-->
    <!--      <el-form ref="form" :model="form" :rules="rules" label-width="80px">-->
    <!--        <el-form-item label="用户姓名" prop="userName">-->
    <!--          <el-input v-model="form.userName" placeholder="请输入用户姓名" />-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="用户手机号" prop="userPhone">-->
    <!--          <el-input v-model="form.userPhone" placeholder="请输入用户手机号" />-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="微信/邮箱/其他" prop="openId">-->
    <!--          <el-input v-model="form.openId" placeholder="请输入微信/邮箱/其他" />-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="需求来源：1.到访、2.网络、3.委托" prop="cluePoolSource">-->
    <!--          <el-input v-model="form.cluePoolSource" placeholder="请输入需求来源：1.到访、2.网络、3.委托" />-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="紧急程度：1.立即入住、2.一周内、3.一个月内、4.先看看" prop="urgencyDegree">-->
    <!--          <el-input v-model="form.urgencyDegree" placeholder="请输入紧急程度：1.立即入住、2.一周内、3.一个月内、4.先看看" />-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="小区地址" prop="premisesAddress">-->
    <!--          <el-input v-model="form.premisesAddress" placeholder="请输入小区地址" />-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="最低价" prop="minimumPrice">-->
    <!--          <el-input v-model="form.minimumPrice" placeholder="请输入最低价" />-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="最高价" prop="topPrice">-->
    <!--          <el-input v-model="form.topPrice" placeholder="请输入最高价" />-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="楼栋数" prop="buildingNumber">-->
    <!--          <el-input v-model="form.buildingNumber" placeholder="请输入楼栋数" />-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="单元数" prop="unitNumber">-->
    <!--          <el-input v-model="form.unitNumber" placeholder="请输入单元数" />-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="层数" prop="floors">-->
    <!--          <el-input v-model="form.floors" placeholder="请输入层数" />-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="房间数" prop="houseNumber">-->
    <!--          <el-input v-model="form.houseNumber" placeholder="请输入房间数" />-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="房屋面积" prop="houseArea">-->
    <!--          <el-input v-model="form.houseArea" placeholder="请输入房屋面积" />-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="免租期(0.否,1.是)" prop="rentHolidayState">-->
    <!--          <el-input v-model="form.rentHolidayState" placeholder="请输入免租期(0.否,1.是)" />-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="B端用户id" prop="busUserId">-->
    <!--          <el-input v-model="form.busUserId" placeholder="请输入B端用户id" />-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="操作人id" prop="operatorId">-->
    <!--          <el-input v-model="form.operatorId" placeholder="请输入操作人id" />-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="备注" prop="remark">-->
    <!--          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="租户id" prop="tenantId">-->
    <!--          <el-input v-model="form.tenantId" placeholder="请输入租户id" />-->
    <!--        </el-form-item>-->
    <!--      </el-form>-->
    <!--      <div slot="footer" class="dialog-footer">-->
    <!--        <el-button type="primary" @click="submitForm">确 定</el-button>-->
    <!--        <el-button @click="cancel">取 消</el-button>-->
    <!--      </div>-->
    <!--    </el-dialog>-->


    <!--  录入  -->
    <add :show.sync="open" :data="form" :title="title" @success="getList" user-type="2" :isSalesman="isSalesman"
         :isDetail="isDetail"/>
    <!--  关闭弹窗  -->
    <close-clue-pool :show.sync="closeDialog" :data="form" @success="getList"/>
  </div>
</template>

<script>
import {
  claimCluePool,
  countCluePoolStatus,
  countCluePoolType,
  listCluePoolByEnterprise
} from "@/api/cluePool/cluePool.js";
import add from '@/views/cluePool/tenant/components/add.vue'
import closeCluePool from '@/views/cluePool/proprietor/components/closeClubPool.vue'

export default {
  name: 'adminClubPool',
  components: {
    add,
    closeCluePool
  },
  dicts: ['push_channel', 'clue_pool_state', 'house_type', 'house_number_type', 'clue_pool_source', 'urgency_degree'],
  data() {
    return {
      // 选中客户类型
      activeClientType: '1',
      // 选中线索池状态
      activeCluePoolState: '0',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 是否销售员
      isSalesman: false,
      // 是否详情
      isDetail: false,
      // 关闭线索池弹窗
      closeDialog: false,
      // 总条数
      total: 0,
      // 线索池信息表格数据
      cluePoolList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cluePoolState: '',//线索池状态：1.正常、2.关闭（字典：clue_pool_state）
        clientType: '1',//客户类型：1.公客、2.私客
        userType: '1',//用户类型：1.租客、2.业主
        userName: '',//用户姓名
        userPhone: '',//用户手机号
        roleType: '1'//角色类型:1.管理员；2.销售员
      },
      // 客户类型列表
      clientTypeList: [
        {label: '公盘', name: '1', value: '0'},
        {label: '私盘', name: '2', value: '0'}
      ],
      // 状态列表
      statusList: [
        {label: '全部', name: '0', value: '0'},
        {label: '正常', name: '1', value: '0'},
        {label: '关闭', name: '2', value: '0'}
      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    console.log(this.$route)
    if (this.$route.name === 'SalesmanClubPool') {
      this.isSalesman = true
      this.queryParams.roleType = '2'//角色类型:1.管理员；2.销售员
    } else if (this.$route.name === 'AdminClubPool') {
      this.isSalesman = false
      this.queryParams.roleType = '1'//角色类型:1.管理员；2.销售员
    }
    this.init()
  },

  methods: {
    /** 查询线索池信息列表 */
    getList() {
      this.loading = true;
      listCluePoolByEnterprise(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.cluePoolList = response.rows;
        this.total = response.total;
        this.loading = false;
      })
      this.getCountCluePoolStatus()
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 初始化 */
    init() {
      this.getList()
      this.getCountCluePoolType()
    },
    // 表单重置
    reset() {
      this.form = {
        cluePoolId: null,
        cluePoolState: null,
        clientType: null,
        userType: '1',
        userName: null,
        userPhone: null,
        sex: null,
        openId: null,
        cluePoolSource: null,
        urgencyDegree: null,
        expectCheckInType: null,
        premisesAddress: null,
        minimumPrice: null,
        topPrice: null,
        houseType: null,
        houseNumberType: null,
        buildingNumber: null,
        unitNumber: null,
        floors: null,
        houseNumber: null,
        houseArea: null,
        decorateSituationType: null,
        rentHolidayState: null,
        busUserId: null,
        createBy: null,
        updateBy: null,
        operatorId: null,
        createTime: null,
        updateTime: null,
        remark: null,
        tenantId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 客户类型切换
    handleClientType(tab) {
      console.log('handleClientType', tab.name)
      this.queryParams.clientType = tab.name
      this.handleQuery()
    },
    // 获取状态数量
    getCountCluePoolType() {
      let params = {
        roleType: this.queryParams.roleType,
        userType: '1'//用户类型：1.租客、2.业主
      }
      countCluePoolType(params).then(response => {
        this.clientTypeList[0].value = response.data['1']
        this.clientTypeList[1].value = response.data['2']
      })
    },
    // 获取状态数量
    getCountCluePoolStatus() {
      let params = {
        clientType: this.queryParams.clientType,
        roleType: this.queryParams.roleType,
        userType: '1'//用户类型：1.租客、2.业主
      }
      countCluePoolStatus(params).then(response => {
        this.statusList[0].value = response.data['1'] + response.data['2']
        this.statusList[1].value = response.data['1']
        this.statusList[2].value = response.data['2']
      })
    },
    handleActiveCluePoolState(tab) {
      console.log('handleActiveCluePoolState', tab.name)
      if (tab.name === '0') {
        this.queryParams.cluePoolState = ''
      } else {
        this.queryParams.cluePoolState = tab.name
      }
      this.handleQuery()
    },
    // 关闭线索池
    handleCloseCluePool(row) {
      this.form = {...row}
      this.closeDialog = true
    },
    handleDetail(row) {
      this.form = {...row}
      this.title = '详情'
      this.isDetail = true
      this.open = true
    },
    handleEdit(row) {
      this.form = {...row}
      this.title = '编辑/激活'
      this.isDetail = false
      this.open = true
    },
    // 认领线索池
    handleClaimCluePool(row) {
      this.$modal.confirm('确定认领到你的私盘池？').then(() => {
        let params = {
          cluePoolId: row.cluePoolId
        }
        return claimCluePool(params)
      }).then((response) => {
        this.$modal.msgSuccess(response.msg || '认领成功')
        this.init()
      }).catch(() => {
      })
    },
    // 转为公盘
    handleTransferToPublic(row) {
      this.$modal.confirm('确定要把客源转为公盘吗？').then(() => {
        let params = {
          cluePoolId: row.cluePoolId
        }
        return claimCluePool(params)
      }).then((response) => {
        this.$modal.msgSuccess(response.msg || '转为公盘成功')
        this.init()
      }).catch(() => {
      })
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.isDetail = false
      this.title = '录入租客'
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.pushRecordId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    }

  }
};
</script>
